package com.baijiayun.live.ui

import android.content.Context
import androidx.fragment.app.Fragment
import android.view.View
import android.view.ViewGroup
import com.baijiayun.live.ui.speakerlist.item.Switchable
import com.baijiayun.live.ui.speakerlist.item.SwitchableType
import com.baijiayun.livecore.context.LPConstants
import com.baijiayun.livecore.context.LiveRoom
import com.baijiayun.livecore.models.imodels.IUserModel
import java.util.regex.Pattern

private const val ASPECT_RATIO_16_9: Double = 16.0 / 9

/**
 * 获取屏幕高度(px)
 */
fun getScreenHeight(context: Context): Int {
    return context.resources.displayMetrics.heightPixels
}

/**
 * 获取屏幕宽度(px)
 */
fun getScreenWidth(context: Context): Int {
    return context.resources.displayMetrics.widthPixels
}

/**
 * 获取屏幕宽高比
 * @param context
 * @return
 */
fun getScreentApectRatio(context: Context): Double {
    return 1.0 * getScreenWidth(context) / getScreenHeight(context)
}

/**
 * 未加载view计算宽高
 * @param view
 * @return
 */
fun unDisplayViewSize(view: View): IntArray {
    val size = IntArray(2)
    val width = View.MeasureSpec.makeMeasureSpec(0,
            View.MeasureSpec.UNSPECIFIED)
    val height = View.MeasureSpec.makeMeasureSpec(0,
            View.MeasureSpec.UNSPECIFIED)
    view.measure(width, height)
    size[0] = view.measuredWidth
    size[1] = view.measuredHeight
    return size
}

/**
 * wrap_content view计算宽高
 * @param view
 * @return
 */
fun atMostViewSize(view: View): IntArray {
    val size = IntArray(2)
    val width = View.MeasureSpec.makeMeasureSpec(Int.MAX_VALUE shr 2,
            View.MeasureSpec.AT_MOST)
    val height = View.MeasureSpec.makeMeasureSpec(Int.MAX_VALUE shr 2,
            View.MeasureSpec.AT_MOST)
    view.measure(width, height)
    size[0] = view.measuredWidth
    size[1] = view.measuredHeight
    return size
}

/**
 * 16：9分辨率
 */
fun isAspectRatioNormal(context: Context): Boolean {
    return ASPECT_RATIO_16_9 == getScreentApectRatio(context)
}

/**
 * >16:9
 * 典型18：9
 */
fun isAspectRatioLarge(context: Context): Boolean {
    return getScreentApectRatio(context) > ASPECT_RATIO_16_9
}

/**
 * <16:9
 * 典型16：10
 */
fun isAspectRatioSmall(context: Context): Boolean {
    return getScreentApectRatio(context) < ASPECT_RATIO_16_9
}

//老师、ppt、辅助摄像头可以在mainVideo区域
fun isMainVideoItem(switchable: Switchable?) = switchable?.switchableType == SwitchableType.PPT || switchable?.switchableType == SwitchableType.MainItem


fun removeSwitchableFromParent(switchable: Switchable) {
    val view = switchable.view ?: return
    val viewParent = view.parent ?: return
    (viewParent as ViewGroup).removeView(view)
}

fun hasParentView(switchable: Switchable):Boolean{
    val view = switchable.view ?: return false
    return view.parent != null
}

fun removeViewFromParent(view: View) {
    val viewParent = view.parent ?: return
    (viewParent as ViewGroup).removeView(view)
}

fun Fragment.canShowDialog(): Boolean = activity?.run {
    !isFinishing && !isDestroyed
} ?: false

/**
 * 手机号匹配检查
 * @param mobiles 手机号
 * @return 是手机号，则返回true
 */
fun isMobileNumber(mobiles: String?): Boolean {
    val p = Pattern.compile("^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$")
    val m = p.matcher(mobiles)
    return m.matches()
}

//管理员..老师或者助教
fun isAdmin(liveRoom: LiveRoom) = liveRoom.currentUser.type == LPConstants.LPUserType.Teacher || liveRoom.currentUser.type == LPConstants.LPUserType.Assistant
fun isUserAdmin(user: IUserModel) = user.type == LPConstants.LPUserType.Teacher || user.type == LPConstants.LPUserType.Assistant
fun getFormatterTime(timeSeconds:Int) = "${String.format("%02d", timeSeconds / 3600)}:${String.format("%02d", timeSeconds / 60 % 60)}:${String.format("%02d", timeSeconds % 60)}"